//
//  SinaPictureView.m
//  oc微博
//
//  Created by 安陆与 on 16/1/24.
//  Copyright © 2016年 安陆与. All rights reserved.
//

#import "SinaPictureView.h"
#import "SinaStatusModel.h"
#import "SinaStatusViewModel.h"

#import "SinaPictureViewCell.h"
// 只要添加了这个宏，就不用带mas_前缀
#define MAS_SHORTHAND
// 只要添加了这个宏，equalTo就等价于mas_equalTo
#define MAS_SHORTHAND_GLOBALS
//这个头文件一定要写在宏定义下面
#import "Masonry.h"

#define itemMargin 5

#define SinaHomeMargin 10



static NSString *SinaPictureViewReuseIdentifier = @"SinaPictureViewReuseIdentifier";

@interface  SinaPictureView() <UICollectionViewDataSource>

@property (nonatomic, strong) NSArray *pictureArray;

@property (nonatomic, strong) UICollectionViewFlowLayout *layout;

@property (nonatomic, assign) CGFloat itemWith;

@end

@implementation SinaPictureView


-(void)setPicture:(NSArray *)picture{

    _pictureArray = picture;

    self.backgroundColor = [UIColor purpleColor];
    
    self.dataSource = self;
    
    CGSize size = [self getsize];
    [self updateConstraintsIfNeeded];
    [self needsUpdateConstraints];
    //在这里更新约束
    [self updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(size.width);
        make.height.equalTo(size.height);
    }];
    
    [self reloadData];
}

//-(void)setPicture:(SinaStatusViewModel *)picture{
//
//    _picture= picture;
//    
//    _pictureArray = _picture.pic_urls;
//    
//    
//    
//    self.backgroundColor = [UIColor purpleColor];
//    
//    self.dataSource = self;
//    
//    CGSize size = [self getsize];
//    [self updateConstraintsIfNeeded];
//    [self needsUpdateConstraints];
//    //在这里更新约束
//    [self updateConstraints:^(MASConstraintMaker *make) {
//        make.width.equalTo(size.width);
//        make.height.equalTo(size.height);
//    }];
//    
//    [self reloadData];
//}

-(CGSize)getsize{
    
   CGFloat itemWidth = ([UIScreen mainScreen].bounds.size.width - 2 * itemMargin - 2 * SinaHomeMargin) / 3;
    
    
    NSInteger count = self.pictureArray.count;
    
    if (count == 1) {
        return CGSizeMake(itemWidth, itemWidth);
    }else if(count == 4){
    
        return CGSizeMake(itemWidth * 2 + itemMargin, itemWidth * 2 + itemMargin);
    }else{
    
        NSInteger row = (count - 1) / 3 + 1;
        
        CGFloat height = row * itemWidth + (row - 1) * itemMargin;
        
        return CGSizeMake([UIScreen mainScreen].bounds.size.width - 2 * itemMargin, height);
    }
    
    
    
}

-(instancetype)initWithFrame:(CGRect)frame {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];

    self =  [super initWithFrame:frame collectionViewLayout:layout];
    
    CGFloat itemWidth = ([UIScreen mainScreen].bounds.size.width - 2 * itemMargin - 2 * SinaHomeMargin) / 3;

    self.dataSource = self;
    
    [self registerClass:[SinaPictureViewCell class] forCellWithReuseIdentifier:SinaPictureViewReuseIdentifier];
    
    
    layout.itemSize = CGSizeMake(itemWidth, itemWidth);
    
    NSLog(@"%f",itemWidth);
    
    layout.minimumInteritemSpacing = itemMargin;
    
    layout.minimumLineSpacing = itemMargin;
    
    return self;



}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//    NSLog(@"1111%zd", _pictureArray.count);
    return _pictureArray.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SinaPictureViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:SinaPictureViewReuseIdentifier forIndexPath:indexPath];
    
    cell.backgroundColor = indexPath.row % 2 == 0 ? [UIColor redColor]:[UIColor blueColor];
    

    
    cell.pictureName = self.pictureArray[indexPath.item];
    
    return cell;
    
}

@end
